package com.lifeng.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * @author:lifeng
 * @date:2020/5/22 10:44
 */
@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(){
        //默认是使用的是java自带的URLConnect
        /*SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(60*1000);//连接超时 毫秒
        requestFactory.setReadTimeout(60*1000);//读超时 毫秒*/


        //可以使用httpclient,需要导入HttpComponentsClient 依赖的jar
        //生成一个设置了连接超时时间、请求超时时间、异常重试次数3次
        /*RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(30000).build();
        HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(config).setRetryHandler(new DefaultHttpRequestRetryHandler(3, false));
        HttpClient httpClient = builder.build();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);*/
        //RestTemplate restTemplate = new RestTemplate(requestFactory);
        RestTemplate restTemplate = new RestTemplate();
        //做个日志拦截器；从性能上考虑，当前屏蔽该功能
        //restTemplate.setInterceptors(Collections.singletonList(new RestTemplateConsumerLogger()));
        return restTemplate;
    }
}
